<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-show="isAdmin"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['signboard:signboard:edit']"
          >修改</el-button
        >
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- <el-table
      v-loading="loading"
      :data="signboardList"
      @selection-change="handleSelectionChange"
    /> -->
    <span class="title">{{ signboardList[0].newsTitle }}</span>
    <span class="details" v-html="text"></span>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="newsTitle">
          <el-input v-model="form.newsTitle"> </el-input>
        </el-form-item>
        <el-form-item label="详情" prop="signboardDuty">
          <editor v-model="form.signboardDuty" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSignboard,
  getSignboard,
  delSignboard,
  addSignboard,
  updateSignboard,
} from "@/api/signboard/signboard";
import { getUserProfile } from "@/api/system/user";
export default {
  name: "Signboard",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: false,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 招牌管理表格数据
      signboardList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        newsTitle: null,
        signboardAsk: null,
        type: 4,
      },
      text: null,
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      isAdmin: false,
    };
  },
  created() {
    this.getList();
  },
  watch: {
    text(val) {
      if (val !== this.currentValue) {
        this.text = val === null ? "" : val;
        if (this.Quill) {
          this.Quill.pasteHTML(this.text);
        }
      }
    },
  },
  methods: {
    /** 查询招牌管理列表 */

    getList() {
      getUserProfile().then((res) => {
        this.loading = true;
        // this.isAdmin = res.data.roles.admin;
        for (let i = 0; i < res.data.roles.length; i++) {
          if (res.data.roles[i].admin) {
            this.isAdmin = res.data.roles[i].admin;
            // this.queryParams.userId = null;
            break;
          }
        }
        if (!this.isAdmin) {
          this.queryParams.userId = res.data.userId;
        }
      });
      listSignboard(this.queryParams).then((response) => {
        this.signboardList = response.rows;
        this.text = this.signboardList[0].signboardDuty;
        // console.log(this.signboardList[0]);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        newsTitle: null,
        newsImage: null,
        newsDesc: null,
        newsTypeClassId: null,
        createBy: null,
        top: null,
        type: 4,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        version: null,
        signboardAsk: null,
        signboardDuty: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加招牌管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSignboard(this.signboardList[0].id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改服务条款";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            console.log(this.form);
            updateSignboard(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSignboard(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除招牌管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delSignboard(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "signboard/signboard/export",
        {
          ...this.queryParams,
        },
        `signboard_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style scoped>
.title {
  width: 100%;
  text-align: center;
  font-size: 30px;
  margin-left: 45%;
}
.details {
  width: 100%;
  overflow: auto;
  text-align: left;
}
</style>
